package com.scm.biz.service._impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.scm.biz.bean.BusinessConstant;
import com.scm.biz.bean.BusinessFile;
import com.scm.biz.bean.Constant;
import com.scm.biz.bean.OrderPattern;
import com.scm.biz.bean.SampleStore;
import com.scm.biz.dao.BusinessConstantDao;
import com.scm.biz.dao.BusinessFileDao;
import com.scm.biz.dao.ConstantDao;
import com.scm.biz.dao.OrderDao;
import com.scm.biz.dao.OrderPatternDao;
import com.scm.biz.dao.SampleStoreDao;
import com.scm.biz.dto.OrderPatternDto;
import com.scm.biz.enums.FileCategoryEnum;
import com.scm.biz.result.OrderPatternResult;
import com.scm.biz.service.BusinessConstantService;
import com.scm.biz.service.BusinessFileService;
import com.scm.biz.service.OrderPatternService;
import com.scm.core.bean.exp.UfdmException;
import com.scm.utils.Const;
import com.scm.utils.UfdmDateUtil;
import com.scm.utils.UploadAndDownUtil;
@Service
public class OrderPatternServiceImpl implements OrderPatternService{
	@Autowired
	OrderPatternDao orderPatternDao;
	@Autowired
	BusinessFileDao businessFileDao;
	@Autowired
	ConstantDao constantDao;
	@Autowired
	BusinessConstantDao businessConstantDao;
	@Autowired
	private BusinessFileService businessFileService;
	@Autowired
	private BusinessConstantService businessConstantService;
	@Autowired
	private SampleStoreDao sampleStoreDao;
	@Autowired
	private OrderDao orderDao;

	@Override
	public void update(OrderPattern orderPattern) {
		orderPatternDao.updateByPrimaryKeySelective(orderPattern);
	}

	@Override
	public void insert(OrderPatternDto orderPatternDto) throws UfdmException {
		OrderPattern orderPattern = new OrderPattern();
		orderPattern.setDetailDescription(orderPatternDto.getDetailDescription());
		orderPattern.setPeriod(orderPatternDto.getPeriod());
		orderPattern.setSeason(orderPatternDto.getSeason());
		orderPattern.setPatternNo(orderPatternDto.getPatternNo());
		orderPattern.setOrderId(orderPatternDto.getOrderId());
		orderPattern.setAccessoryDetail(orderPatternDto.getAccessoryDetail());
		orderPattern.setDesignId(orderPatternDto.getBusinessId());
		orderPattern.setDetailDescription(orderPatternDto.getDetailDescription());
		orderPattern.setCode(UfdmDateUtil.getCurrentDate("yyyyMMddHHmmss"));
		orderPattern.setCreatedAt(new Date());
		orderPattern.setSex(orderPatternDto.getSex());
		
		//保存
		
		orderPatternDao.insert(orderPattern);
		Long businessId = orderPattern.getId();
		String mainImage="";
		if(orderPatternDto.getImageFile() !=null){
			BusinessFile businessFile = null;
			for(MultipartFile imagFile:orderPatternDto.getImageFile()){
				String filePath = UploadAndDownUtil.uploadImage(imagFile, FileCategoryEnum.ORDER_PATTERN_IMAGE.value());
				businessFile = new BusinessFile();
				businessFile.setBusinessFile(filePath);
				businessFile.setBusinessId(businessId);
				businessFile.setCategory(FileCategoryEnum.ORDER_PATTERN_IMAGE.value());
				businessFile.setCreatedAt(new Date());
				businessFileDao.insert(businessFile);
				mainImage = filePath;
				
			}
		}
		
		orderPattern.setMainImage(mainImage);
		orderPatternDao.updateByPrimaryKeySelective(orderPattern);
		for(Map<String, String> styleLabel:orderPatternDto.getStyleLabels()){
			Constant constant = constantDao.selectCodeByName(styleLabel.get("name"), FileCategoryEnum.ORDER_PATTERN_STYLELABLE.toString());
			if(constant==null){
				constant = new Constant();
				constant.setCategory(FileCategoryEnum.ORDER_PATTERN_STYLELABLE.value());
				constant.setCode(UfdmDateUtil.getCurrentDate("yyyyMMddHHmmss"));
				constant.setName(styleLabel.get("name"));
				constant.setCreated_at(new Date());
				constantDao.insert(constant);
			}
			BusinessConstant businessConstant = new BusinessConstant();
			businessConstant.setCreated_at(new Date());
			businessConstant.setBusiness_id(orderPattern.getId());
			businessConstant.setCategory(FileCategoryEnum.ORDER_PATTERN_STYLELABLE.value());
			businessConstant.setConstant_id(constant.getId());
			businessConstantDao.insert(businessConstant);
		}
		
	}

	@Override
	public void submitVerify(Long businessId) {
		
	}

	@Override
	public List<OrderPatternResult> selectListByOrderId(Long orderId) {
		return orderPatternDao.selectListByOrderId(orderId);
	}

	@Override
	public OrderPatternResult selectById(Long id) {
		return orderPatternDao.selectById(id);
	}

	@Override
	public OrderPatternResult getById(Long id) {
		return orderPatternDao.selectByPatternId(id);
	}

	@Override
	public void modify(OrderPattern orderPattern,MultipartHttpServletRequest req) throws Exception {
		String style=req.getParameter("style");
		if(style==null||style.trim().isEmpty()){
			throw new UfdmException("设计稿风格为空");
		}
		String[] styles=style.split(Const.SPLIT);
		if(styles.length==0){
			throw new UfdmException("设计稿风格为空");
		}
		List<MultipartFile> images=req.getFiles("images");
		String[] index=req.getParameterValues("index");
		if(index!=null){
			if(images.size()+index.length>5){
				throw new UfdmException("设计稿图片超过5张");
			}
			if(images.size()==0&&index.length==0){
				throw new UfdmException("设计稿无图片");
			}
		}else{
			if(images.size()>5){
				throw new UfdmException("设计稿图片超过5张");
			}
			if(images.size()==0){
				throw new UfdmException("设计稿无图片");
			}
		}
		businessConstantDao.deleteByCustomerId(orderPattern.getId().intValue(), FileCategoryEnum.ORDER_PATTERN_STYLELABLE.value());
		List<BusinessFile> list=businessFileDao.selectByBusinessId(orderPattern.getId(), FileCategoryEnum.ORDER_PATTERN_IMAGE.value());
		String main=req.getParameter("main");
		if(main!=null){
			int mainImage=new Integer(main);
			String image=list.get(mainImage).getBusinessFile();
			orderPattern.setMainImage(image);
		}
		if(index!=null){
			for(int i=index.length-1;i>=0;i--){
				list.remove(new Integer(index[i]).intValue());
			}
		}
		for(BusinessFile file:list){
			businessFileDao.deleteByPrimaryKey(file.getId());
			UploadAndDownUtil.removeFile(file.getBusinessFile());
		}
		String mainImg=req.getParameter("mainImage");
		int mainImage=-1;
		String image=null;
		if(mainImg!=null){
			mainImage=new Integer(mainImg);
			image=UploadAndDownUtil.uploadImage(images.get(mainImage), FileCategoryEnum.ORDER_PATTERN_IMAGE.value());
			orderPattern.setMainImage(image);
		}
		orderPatternDao.updateByPrimaryKeySelective(orderPattern);
		businessConstantService.save(style, orderPattern.getId(), FileCategoryEnum.ORDER_PATTERN_STYLELABLE.value());
		businessFileService.saveImage(images, orderPattern.getId().intValue(),FileCategoryEnum.ORDER_PATTERN_IMAGE.value(),mainImage,image);
	}

	@Override
	public void save(OrderPattern pattern, MultipartHttpServletRequest req) throws UfdmException {
		String style=req.getParameter("style");
		if(style==null||style.trim().isEmpty()){
			throw new UfdmException("风格为空");
		}
		String[] styles=style.split(Const.SPLIT);
		if(styles.length==0){
			throw new UfdmException("风格为空");
		}
		List<MultipartFile> images=req.getFiles("images");
		if(images.size()>5){
			throw new UfdmException("图片超过5张");
		}
		if(images.size()==0){
			throw new UfdmException("无图片");
		}
		String mainImg=req.getParameter("mainImage");
		if(mainImg==null) mainImg="0";
		int mainImage=new Integer(mainImg);
		String orderCode=orderDao.getOrderCodeById(pattern.getOrderId());
		int count=orderDao.selectPatternCountById(pattern.getOrderId());
		String image=UploadAndDownUtil.uploadImage(images.get(mainImage), FileCategoryEnum.ORDER_PATTERN_IMAGE.value());
		pattern.setCode(Const.getSubOrderCode(orderCode,++count));
		pattern.setPatternNo(Const.getSampleCode());
		pattern.setMainImage(image);
		pattern.setCreatedAt(new Date());
		orderPatternDao.insert(pattern);
		businessConstantService.save(style, pattern.getId(), FileCategoryEnum.ORDER_PATTERN_STYLELABLE.value());
		businessFileService.saveImage(images, pattern.getId().intValue(),FileCategoryEnum.ORDER_PATTERN_IMAGE.value(),mainImage,image);
		//保存样衣
		image=UploadAndDownUtil.copyImage(image, FileCategoryEnum.SAMPLE_IMAGE.value());
		SampleStore sample=new SampleStore();
		sample.setCode(pattern.getPatternNo());
		sample.setCreateAt(new Date());
		sample.setMainImage(image);
		sample.setSex(pattern.getSex());
		sample.setSource("集合智造");
		sample.setSeason(pattern.getSeason()+"款");
		sampleStoreDao.insert(sample);
		businessConstantService.save(style, sample.getId(), FileCategoryEnum.SAMPLE_STYLE.value());
		List<String> patternImages=businessFileDao.selectImage(pattern.getId(), FileCategoryEnum.ORDER_PATTERN_IMAGE.value());
		businessFileService.copyImage(patternImages, sample.getId().longValue(),FileCategoryEnum.SAMPLE_IMAGE.value(),mainImage,image);
	}

}
